<?php
namespace app\admin\controller;
use think\Controller;

class Index extends Controller
{
    public function index()
    {

        $id = session('adminuser.id');
        if(empty($id)){
            $this->redirect('index/login');
        }
        $data = Db('admin_user')->where('id',$id)->find();
        $this->assign('data',$data);
        return view('index');
    }

    /**
     * 登录界面
     */
    public function login(){
        return view('login/index');
    }

    /**
     * 登录验证
     */
    public function do_login($code=''){
        if(request()->isPost()){
            $post = input('post.');
            if(!captcha_check($post['verifyCode'])) {
                // 校验失败
                return $this->error('验证码不正确');
            }
            // $data=array();
            // $this->validate($data,[
            // 'captcha|验证码'=>'require|captcha'
            // ]);
            $password = Db('admin_user')->where('username',$post['username'])->value('password');
            $id = Db('admin_user')->where('username',$post['username'])->find();
            if($password){
                if($password == md5($post['password'])){
                    session('adminuser',$id);
                    return $this->redirect('index/index');
                }else{
                    return $this->error('密码不正确');
                }
            }else{
                return $this->error('用户名不正确');
            }


        }
    }



    /**
     * 用户退出
     */
    public function login_out(){
        session('adminuser',null);
        return view('login/index');
    }

}
